﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBFEHLController 
    {
        [ChildActionOnly]
        public ActionResult MWB19X(string returnUrl)
        {
            ModelState.Clear();
            MWB.INFO = "";
            MWB.INFO = INFO;
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(MABF.CWBB.CERK.ERRORS);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MWB19X(WBFEHLModel model, string returnUrl)
        {

            try
            {   
                MWB.INFO = "";
                SetUpChart();
            }
            catch (Exception ex)
            {
                //VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        [ChildActionOnly]
        public ActionResult PWB19X(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if(MABF.CWBB.CERK.CERROR==null)
            {
                StartNewError();
            }
            this.ViewData["list"] = ERKEYES;
            return PartialView(MABF.CWBB.CERK.CERROR);
        }
        [HttpPost]
        [AllowAnonymous]
        public ActionResult PWB19X(WBFEHLModel model, string returnUrl)
        {
            try
            {
                StartNewJob(model);//开始新疵点  

            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }

        private void SetUpChart()
        {
            return;
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBWAKZ"] = MWB.WAKZ;
            dwData["WBBART"] = MWB.ART;
            dwData["WBBVAR"] = MWB.VAR;
            dwData["WBBFARBE"] = MWB.FARBE;
            DataSet SResult = VHP.VWBB.QWB25(dwData);
            MWB.INFO = "";
            MWB.WHPCS.Clear();
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                MWB.WHPCS.Add(row["WBBLB"].ToString(), row["PCS"].ToString());
                MWB.INFO += row["WBBLB"].ToString() + row["PCS"].ToString() + ";";
            }
        }
        public void StartNewJob(WBFEHLModel model)
        {
            if (model != null)
            {
                var result = MABF.CWBB.CERK.ERRORS.Where(p => p.EPOS == MABF.CWBB.CERK.CERROR.EPOS);
                foreach(WBFEHLModel error in result)
                {
                    error.EKEY = model.EKEY;
                    error.EPOINTS = model.EPOINTS;
                    error.WBFLGVON = model.WBFLGVON;
                    error.WBFLGBIS = model.WBFLGBIS;
                    error.EKEY = model.EKEY;
                }
                MABF.CWBB.CERK.CERROR.EKEY = model.EKEY;
                MABF.CWBB.CERK.CERROR.dwMaster["WBFQPKT"] = model.EPOINTS;
                MABF.CWBB.CERK.CERROR.dwMaster["WBFLGVON"] = model.WBFLGVON;
                MABF.CWBB.CERK.CERROR.dwMaster["WBFLGBIS"] = model.WBFLGBIS;
                MABF.CWBB.CERK.CERROR.dwMaster["WBFFSCHL"] = model.EKEY;
                VHP.VWBFE.UpdateWBFEHL(MABF.CWBB.CERK.CERROR.dwMaster, 0);
            }
            StartNewError();

        }
        public void StartNewError()
        {
            if (MABF == null)
                return;
            if (MABF.CWBB.CERK == null)
                return;
            WBFEHLModel m;
            MABF.CWBB.CERK.EKPOS++;
            PM["RWKRS"] = MABF.CWBB.RWKRS;
            PM["RWLFD"] = MABF.CWBB.RWLFD;
            PM["RWPOS"] = MABF.CWBB.RWPOS;
            PM["ERVER"] = MABF.CWBB.CERK.ERVER;
            PM["FPOS"] = MABF.CWBB.CERK.EKPOS;
            PM["QPKT"] = 4;
            m = new WBFEHLModel();
            m.ErrorList = ERKEYES;
            m.dwMaster = VHP.VWBFE.NEWERROR(PM);
            m.EKEY = "001";
            m.EPOS = MABF.CWBB.CERK.ERVER + "." + MABF.CWBB.CERK.EKPOS;
            m.EPOINTS = "";
            MABF.CWBB.CERK.ERRORS.Add(m);
            MABF.CWBB.CERK.CERROR = m;
        }
    
    }
}
